package com.jltx.xq.relaxnewspro;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.jltx.xq.relaxnewspro.fragment.BaseCollectionFragment;
import com.jltx.xq.relaxnewspro.fragment.BaseNewsFragment;
import com.jltx.xq.relaxnewspro.fragment.BaseReadingFragment;
import com.jltx.xq.relaxnewspro.fragment.BaseScienceFragment;
import com.jltx.xq.relaxnewspro.fragment.DailyFragment;
import com.jltx.xq.relaxnewspro.framework.BaseAppCompatActivity;
import com.jltx.xq.relaxnewspro.utils.ViewHelper;

public class MainActivity extends BaseAppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private Toolbar toolbar1;
    private NavigationView  navigationView;
    private Fragment currentFragment;
    private FragmentTransaction fragmentTransaction;
    private Menu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        toolbar1 = ViewHelper.findViewById(this,R.id.toolbar1);
        setSupportActionBar(toolbar1);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);

        mDrawerLayout = ViewHelper.findViewById(this,R.id.drawer_layout);
        navigationView = ViewHelper.findViewById(this,R.id.nav_view);
        if(navigationView != null){
            setupDrawerContent(navigationView);
        }
        //Home旋转开关按钮实际上是通过ActionBarDrawerToggle代码绑定到toolbar上的，ActionBarDrawerToggle是和DrawerLayout搭配使用的
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar1, R.string.app_name, R.string.app_name);
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.daily:
                        if (currentFragment instanceof DailyFragment) {
                            return false;
                        }
                        currentFragment = new DailyFragment();
                        break;
                    case R.id.science:
                        if (currentFragment instanceof BaseScienceFragment) {
                            return false;
                        }
                        currentFragment = new BaseScienceFragment();
                        break;
                    case R.id.journalism:
                        if(currentFragment instanceof BaseNewsFragment){
                            return false;
                        }
                        currentFragment = new BaseNewsFragment();
                        break;
                    case R.id.collection:
                        if(currentFragment instanceof BaseCollectionFragment){
                            return false;
                        }
                        currentFragment = new BaseCollectionFragment();
                        break;
                    case R.id.read:
                        if(currentFragment instanceof BaseReadingFragment){
                            return false;
                        }
                        currentFragment = new BaseReadingFragment();
                        break;
                }
                item.setChecked(true);
                mDrawerLayout.closeDrawers();
                switchFragment();
                return false;
            }
        });
//        this.setTheme(R.style.DayTheme);

        navigationView.setCheckedItem(R.id.daily);
        currentFragment = new DailyFragment();
        switchFragment();



    }

    private void switchFragment(){
        if(currentFragment instanceof DailyFragment){
            switchFragment(currentFragment,getString(R.string.daily),R.menu.menu_daily);
        }else if(currentFragment instanceof BaseScienceFragment){
            switchFragment(currentFragment,getString(R.string.science),R.menu.menu_science);
        }else if(currentFragment instanceof BaseNewsFragment){
            switchFragment(currentFragment,getString(R.string.news),R.menu.menu_news);
        }else if(currentFragment instanceof BaseCollectionFragment){
            switchFragment(currentFragment,getString(R.string.collection),R.menu.menu_daily);
        }else if(currentFragment instanceof BaseReadingFragment){
            switchFragment(currentFragment,getString(R.string.reading),R.menu.menu_reading);
        }
    }

    private void switchFragment(Fragment fragment,String title,  int resourceMenu) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.framelayout,fragment);
        fragmentTransaction.commit();
        getSupportActionBar().setTitle(title);
        if(menu != null){
            menu.clear();
            getMenuInflater().inflate(resourceMenu,menu);
        }
    }

    private void setupDrawerContent(NavigationView navigationView){

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         this.menu =  menu;
        getMenuInflater().inflate(R.menu.menu_daily,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;

            case R.id.menu_science:
                navigationView.setCheckedItem(R.id.science);
                currentFragment = new BaseScienceFragment();
                break;
            case R.id.menu_home:
                navigationView.setCheckedItem(R.id.daily);
                currentFragment = new DailyFragment();
                break;
            case R.id.menu_news:
                navigationView.setCheckedItem(R.id.menu_reading);
                currentFragment = new BaseNewsFragment();
                break;

            default:
                break;
        }
        switchFragment();
        return true;

    }
}
